package org.xin.security.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.map.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.xin.framework.constant.PermissionType;
import org.xin.security.component.TokenComponent;
import org.xin.security.pojo.bo.SecurityUser;
import org.xin.security.pojo.bo.UserInfo;
import org.xin.security.pojo.entity.SysMenu;
import org.xin.security.mapper.SysMenuMapper;
import org.xin.security.pojo.entity.SysRolePermission;
import org.xin.security.pojo.vo.SysMenuTreeVO;
import org.xin.security.service.ISysMenuSecurityService;
import org.xin.security.service.ISysMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import org.xin.framework.base.service.impl.IBaseServiceImpl;
import org.xin.security.service.ISysRolePermissionService;
import org.xin.security.service.ISysUserService;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * <p>
 * 系统模块-菜单权限表 服务实现类
 * </p>
 *
 * @author: xin
 * @date: Create in 2021-09-29
 */
@Slf4j
@Service
public class SysMenuServiceImpl extends IBaseServiceImpl<SysMenu, SysMenuMapper> implements ISysMenuService {
    private final TokenComponent tokenComponent;
    private final ISysUserService userService;
    private final ISysMenuSecurityService menuSecurityService;
    private final ISysRolePermissionService rolePermissionService;

    public SysMenuServiceImpl(ISysUserService userService, ISysMenuSecurityService menuSecurityService, TokenComponent tokenComponent, ISysRolePermissionService rolePermissionService) {
        this.userService = userService;
        this.menuSecurityService = menuSecurityService;
        this.tokenComponent = tokenComponent;
        this.rolePermissionService = rolePermissionService;
    }



    //TODO 有什么好的方案获取菜单树？
    @Override
    public List<Tree<String>> buildMenuTreeSelect(List<SysMenu> sysMenus) {
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig()
        // 最大递归深度
        .setDeep(3);

        // 构建node列表
        List<TreeNode<String>> nodeList = CollUtil.newArrayList();
        sysMenus.forEach(sysMenu -> {
            nodeList.add(new TreeNode<String>().setExtra(BeanUtil.beanToMap(sysMenu)));
        });

        //转换器
        return TreeUtil.build(nodeList, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    Map<String, Object> extra = treeNode.getExtra();
                    for (Map.Entry<String, Object> entry : extra.entrySet()) {
                        tree.putExtra(entry.getKey(), entry.getValue());
                    }
                });
    }

    @Override
    public List<SysMenu> selectMenusByLoginUser(HttpServletRequest request) {

        SecurityUser securityUser = userService.loginUserInfo(request);
        UserInfo userInfo = securityUser.getUser();

        //获取到该角色的权限
        Set<String> rolePermissions = rolePermissionService.selectPermissionByRoleId(new HashSet<>(), userInfo.getSysRoleId(), PermissionType.MENU.ordinal());
        return new ArrayList<>() {{
            rolePermissions.forEach(permission -> {
                menuSecurityService.selectByPermission(permission).forEach(sysMenuSecurity -> {
                    this.add(getById(sysMenuSecurity.getSysMenuId()));
                });
            });
        }};
    }

}
